//设置依赖的配置文件
require.config({
	paths : {
		"jquery" : "jquery",
		"share_top" : "share_top",
		"share_bottom" : "share_bottom",
		"cookie" : "cookie"
	}
})
//设置依赖
require(["jquery","share_top","share_bottom","cookie"],function($,share_top,share_bottom,cookiefn){
	$("#top_box").load("share_top.html",function(){
		share_top.fn();
		$(".top_cart .num span").html(cookiefn.getTotal());
	});
	$("#bottom_box").load("share_bottom.html",function(){
		share_bottom.fn();
	});
	$.getJSON('../json/list_goods.json',function(response,status,xhr){
		$(".classify em").html(response[0].message);
		$(".price_sale .price i").html(response[0].price);
		$(".price_sale .sale b").html(response[0].sale_message);
		$(".details_date p:nth-child(2) span").html(response[0].num);
		//随机函数
		function getRandom(min,max){
			return Math.floor(Math.random() * (max - min) + min);
		}
		//右侧浮动购物车详情创建
		function getRightCartImg(){
			$arr = [];
			$(".right_cart ol").empty();
			while(1){
				$other_index = getRandom(0,39);
				if($arr.indexOf($other_index) == -1){
					$arr.push($other_index);
					$(".right_cart ol").append('<li><a href="#"><img src="../' + response[$other_index].paths1 + '"/><p>￥' + response[$other_index].price + '</p></a></li>')
				}
				if($arr.length == 4){
					break;
				}
			}
		}
		$("#how_many").val(1);
		$(".right_cart em").click(function(){
			$(".right_cart").css("display","none");
		})
		//增加减少数量
		$(".add_num").click(function(){
			$("#how_many").val($("#how_many").val() - 0 + 1);
		})
		$(".sub_num").click(function(){
			if($("#how_many").val() == 1){
				alert("商品数量不得小于1");
			}else{
				$("#how_many").val($("#how_many").val() - 1);
			}
		})
		//加入购物车点击事件
		$(".add_btn").click(function(event){
			var n = parseInt($("#how_many").val());
			if(cookiefn.getNum(1,"goodNum")){
				num = parseInt(cookiefn.getNum(1,"goodNum")) + n;
			}else{
				num = n;
			}
			cartMove(event,num);
			
		})
		function cartMove(event,n){
			//弹出购物车弹框
			$(".right_cart").css("display","block");
			getRightCartImg();
			//飞入购物车
			var cloneImg = $(".details_gocart_img").clone(true).appendTo('body').css({
				"display":"block",
				"left":event.pageX,
				"top":event.pageY - $(window).scrollTop(),
				"z-index":11111
				});
			cloneImg.animate({
				"left":$(".right_cart_btn").offset().left + 10,
				"top":$(".right_cart_btn").offset().top + 20 - $(window).scrollTop(),
				"width":10,
				"height":20,
				"opacity":0.5
			},1000,function(){
				setTimeout(function(){
					cloneImg.remove();
				},50)
			});
			//{"src":"../img/list/list_1_1.jpg","message":"53度 茅台","price":"354","num":"12","sale":"立减20"}
			//创建并获取cookie
			var cooSrc = "../" + response[0].paths1;
			var cooMsg = response[0].message;
			var cooPrice = response[0].price;
			var cooEva = response[0].num;
			//var cooSale = $obj.parent().find(".sale_message").html();
			var cooNum = n;
			//alert(cooNum);
			var cooId = response[0].goods_id;
			var cooKey = response[0].goods_id;
			//alert(cooKey);
			var cookieText = '{"goodSrc":"' + cooSrc + '","goodMsg":"' + cooMsg + '","goodPrice":"' + cooPrice + '","goodEva":"' + cooEva + '","goodNum":"' + cooNum + '","goodId":"' + 1 + '"}';
			cookiefn.createCookie(cooKey,cookieText,7,"/");
			var total = cookiefn.getTotal();
			$(".top_cart .num span").html(total);
			//alert(total);
		}
	})
	//放大镜--移入小图切换大图
	$(".little_img_box li").mouseover(function(){
		$(".little_img_box li").css({
			"padding":"1px",
			"border":"1px solid #eee"
		})
		$(this).css({
			"padding":0,
			"border":"2px solid #dc3334"
		})
		$(".big_img_1 img").attr("src","../img/details/big_" + ($(this).index() + 1) + ".jpg");
		$(".big_img_2 img").attr("src","../img/details/big_" + ($(this).index() + 1) + ".jpg");
	})
	//放大镜
	$(".cover").mouseover(function(event){
		$(".big_img_2").css("display","block");
		beMax(event);
	})
	$(".cover").mouseout(function(event){
		$(".big_img_2").css("display","none");
	})
	$(".cover").mousemove(function(event){
		beMax(event);
	})
	function beMax(event){
		$x = $(".big_img_2 img").width() / $(".big_img_2").width();
		$y = $(".big_img_2 img").height() / $(".big_img_2").height();
		$l = -(event.pageX - $(".big_img_2").offset().left) * ($x - 1);
		$t = -Math.abs((event.pageY - $(".big_img_2").offset().top)) * ($y - 1);
		$(".big_img_2 img").css({
			"left":$l,
			"top":$t
		});
	}
})